package com.sz.common.base.utils;

import java.math.BigDecimal;

/**
 * Function: TODO: ADD FUNCTION <br>
 * Author: Charles. <br>
 * Date: 2017-02-23 08:59:00.
 */
public class TypeConversionUtils {

    public static final String BOOLEAN = "boolean";
    public static final String INT = "int";
    public static final String LONG = "long";
    public static final String ZERO = "0";
    public static final String FLOAT = "float";
    public static final String DOUBLE = "double";
    public static final String SHORT = "short";

    /**
     * @param obj
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T covertTo(Object obj, Class<T> clazz) {
        if (clazz == null) {
            throw new IllegalArgumentException("class cannot be empty");
        }
        if (clazz.isPrimitive()) { //native 类型， 不能返回null
            return (T) covertToPrimitiveType(obj, clazz.getName());
        }
        if (obj == null) {
            return null;
        }
        Object val = obj;
        if (!clazz.isInstance(val)) {
            if (clazz.equals(String.class)) {
                val = obj.toString();
            } else if (clazz.equals(Integer.class)) {
                try {
                    val = Integer.valueOf(obj.toString());
                } catch (NumberFormatException ex) {
                    val = 0;
                }
            } else if (clazz.equals(Long.class)) {
                try {
                    val = Long.valueOf(obj.toString());
                } catch (NumberFormatException ex) {
                    val = 0L;
                }
            } else if (clazz.equals(Float.class)) {
                try {
                    val = new Float(obj.toString());
                } catch (NumberFormatException ex) {
                    val = 0f;
                }
            } else if (clazz.equals(Double.class)) {
                try {
                    val = new Double(obj.toString());
                } catch (NumberFormatException ex) {
                    val = 0.0;
                }
            } else if (clazz.equals(Boolean.class)) {
                val = Boolean.valueOf(obj.toString());
            } else if (clazz.equals(BigDecimal.class)) {
                try {
                    val = new BigDecimal(obj.toString());
                } catch (NumberFormatException ex) {
                    val = 0.0;
                }
            }
        }
        return (T) val;
    }

    public static Object covertToPrimitiveType(Object obj, String clazz) {
        if (BOOLEAN.equalsIgnoreCase(clazz)) {
            return obj != null && Boolean.parseBoolean(obj.toString());
        } else if (INT.equalsIgnoreCase(clazz)) {
            try {
                return Integer.parseInt(obj == null ? ZERO : obj.toString());
            } catch (NumberFormatException ex) {
                return 0;
            }
        } else if (LONG.equalsIgnoreCase(clazz)) {
            try {
                return Long.parseLong(obj == null ? ZERO: obj.toString());
            } catch (NumberFormatException ex) {
                return 0L;
            }
        } else if (FLOAT.equalsIgnoreCase(clazz)) {
            try {
                return Float.parseFloat(obj == null ? ZERO: obj.toString());
            } catch (NumberFormatException ex) {
                return 0.0f;
            }
        } else if (DOUBLE.equalsIgnoreCase(clazz)) {
            try {
                return Double.parseDouble(obj == null ? ZERO: obj.toString());
            } catch (NumberFormatException ex) {
                return 0.0d;
            }
        } else if (SHORT.equalsIgnoreCase(clazz)) {
            try {
                return Short.parseShort(obj == null ? ZERO: obj.toString());
            } catch (NumberFormatException ex) {
                return 0;
            }
        }
        return 0;
    }
}
